package domain;

import org.apache.ibatis.session.SqlSession;

import java_persistence.DoctorMapper;
import java_persistence.NurseMapper;
import java_persistence.OrdersMapper;
import java_persistence.TreatmentRecordMapper;

/**
 * @author joseph simoneau <simoneau@gatech.edu>
 *
 */
public class TreatmentRecord {

	private Doctor attending_doctor;
	private Nurse attending_nurse;
	private Integer bp_systolic;
	private Integer bp_diastolic;
	private Integer pulse;
	private Float temperature;
	private Integer height_inches;
	private Integer weight_pounds;
	private String diagnosis;
	private String notes;
	private Orders orders;
	private Invoice invoice;

	public TreatmentRecord() {
		super();
	}
	
	public TreatmentRecord( Doctor d, Nurse n, int bps, int bpd, int p, float t, int hi, int wp, String di, String no, Orders o, Invoice i) {
		this.attending_doctor = d;
		this.attending_nurse = n;
		this.bp_systolic = bps;
		this.bp_diastolic = bpd;
		this.pulse = p;
		this.temperature = t;
		this.height_inches = hi;
		this.weight_pounds = wp;
		this.diagnosis = di;
		this.notes = no;
		this.orders = o;
		this.invoice = i;
	}

	public Doctor getAttending_doctor() {
		return attending_doctor;
	}
	public Nurse getAttending_nurse() {
		return attending_nurse;
	}
	public int getBp_systolic() {
		return bp_systolic;
	}
	public int getBp_diastolic() {
		return bp_diastolic;
	}
	public int getPulse() {
		return pulse;
	}
	public float getTemperature() {
		return temperature;
	}
	public int getHeight_inches() {
		return height_inches;
	}
	public int getWeight_pounds() {
		return weight_pounds;
	}
	public String getDiagnosis() {
		return diagnosis;
	}
	public String getNotes() {
		return notes;
	}
	public Orders getOrders() {
		return orders;
	}
	public Invoice getInvoice() {
		return invoice;
	}

	public void setAttending_doctor(Doctor attending_doctor) {
		this.attending_doctor = attending_doctor;
	}
	public void setAttending_nurse(Nurse attending_nurse) {
		this.attending_nurse = attending_nurse;
	}
	public void setBp_systolic(int bp_systolic) {
		this.bp_systolic = bp_systolic;
	}
	public void setBp_diastolic(int bp_diastolic) {
		this.bp_diastolic = bp_diastolic;
	}
	public void setPulse(int pulse) {
		this.pulse = pulse;
	}
	public void setTemperature(float temperature) {
		this.temperature = temperature;
	}
	public void setHeight_inches(int height_inches) {
		this.height_inches = height_inches;
	}
	public void setWeight_pounds(int weight_pounds) {
		this.weight_pounds = weight_pounds;
	}
	public void setDiagnosis(String diagnosis) {
		this.diagnosis = diagnosis;
	}
	public void setNotes(String notes) {
		this.notes = notes;
	}
	public void setOrders(Orders orders) {
		this.orders = orders;
	} 
	public void setInvoice(Invoice invoice) {
		this.invoice = invoice;
	}
	
	
	
	// Database Functionality
	
	// CREATES
	
	/**Stores 'this''s information in the database. 
	 * @param a the associated Appointment.
	 * @param session an open, valid SqlSession object.  
	 */
	public void create_treatment_record(Appointment a, SqlSession session) {
		TreatmentRecordMapper trmap = session.getMapper(TreatmentRecordMapper.class);
		
		if( a.getDate()!= null && a.getPatient().getUsername()!=null ){
			trmap.create_treatment_record( a.getDate(), a.getPatient().getUsername(), 
										   this.attending_doctor.getUsername(), 
										   this.attending_nurse.getUsername(),
										   this.bp_systolic,
										   this.bp_diastolic,
										   this.pulse,
										   this.temperature,
										   this.height_inches,
										   this.weight_pounds,
										   this.diagnosis,
										   this.notes );
		}
	}
	
	// RETRIEVES
	
	/**Retrieves the treatment record for an appointment on 'date' for patient 'patient_username'
	 * @param date the associated date
	 * @param patient_username the patient in question's username
	 * @param session a valid, open SqlSession object.
	 */
	public void retrieve (String date, String patient_username, SqlSession session) {
		String dr_username = session.getMapper(TreatmentRecordMapper.class).get_attending_doctor_username(date, patient_username);
		String nurse_username = session.getMapper(TreatmentRecordMapper.class).get_attending_nurse_username(date, patient_username);
		this.attending_doctor = session.getMapper(DoctorMapper.class).get_doctor_by_username(dr_username);
		this.attending_nurse = session.getMapper(NurseMapper.class).get_nurse_by_username(nurse_username);
		TreatmentRecordMapper trmap = session.getMapper(TreatmentRecordMapper.class);
		this.bp_systolic = trmap.get_bp_systolic(date, patient_username);
		this.bp_diastolic = trmap.get_bp_diastolic(date, patient_username);
		this.pulse = trmap.get_pulse(date, patient_username);
		this.temperature = trmap.get_temp(date, patient_username);
		this.height_inches = trmap.get_height_inches(date, patient_username);
		this.weight_pounds = trmap.get_weight_pounds(date, patient_username);
		this.diagnosis = trmap.get_diagnosis(date, patient_username);
		this.notes = trmap.get_notes(date, patient_username);
		this.orders = session.getMapper(OrdersMapper.class).retrieve(date, patient_username);
		
		this.invoice = new Invoice();
		this.invoice.setMyServices(trmap.get_invoice(date, patient_username));
		
	}
	
	// UPDATES
	
	/**Updates 'this''s enty in the database.
	 * @param a the associated Appointment
	 * @param trmap retrieved from a valid, open SqlSession object.
	 */
	public void update_treatment_record(Appointment a, TreatmentRecordMapper trmap) {
		if( a.getDate()!= null && a.getPatient().getUsername()!=null ){
			trmap.update_treatment_record( a.getDate(), a.getPatient().getUsername(), 
										   this.attending_doctor.getUsername(), 
										   this.attending_nurse.getUsername(),
										   this.bp_systolic,
										   this.bp_diastolic,
										   this.pulse,
										   this.temperature,
										   this.height_inches,
										   this.weight_pounds,
										   this.diagnosis,
										   this.notes );
		}
		
		if( this.invoice != null ) {
			for( Services s: invoice.getMyServices() ) {
				trmap.add_service_to_treatment_record(a.getDate(), a.getPatient().getUsername(), s.getService_name());
			}
		}
	}
	
	
	
	
	// DELETES
	
	/**Deletes 'this''s entry from the database.
	 * @param a the associated Appointment
	 * @param trmap retrieved from a valid, open SqlSession object.  
	 */
	public void delete_treatment_record( Appointment a, TreatmentRecordMapper trmap) {
		trmap.delete_treatment_record( a.getDate(), a.getPatient().getUsername() );
	}
	
	

}
